package com.berwin.syncer.views;

import com.berwin.syncer.entry.Syncer;
import com.berwin.syncer.layout.VerticalFlowLayout;
import com.berwin.syncer.utils.UserDefault;
import com.berwin.syncer.utils.Utility;

import javax.swing.*;
import java.awt.*;
import java.io.File;

public class SettingsView extends JFrame {

    public static final String KEY_PORT_TEXT = "key_port_text";
    public static final String KEY_PORT_FILE = "key_port_file";
    public static final String KEY_MAX_ITEM = "key_max_item";
    public static final String KEY_DOWNLOAD_DIR = "key_download_dir";
    public static final String KEY_MONITOR_INTERVAL = "key_monitor_interval";

    public interface OnConfigListener {
        void onConfigChanged(int portText, int portFile, int maxCache, String downloadDir, int monitorInterval);
    }

    private OnConfigListener listener;

    public SettingsView(OnConfigListener listener) {
        this.listener = listener;
        this.setTitle("系统偏好设置");
        this.setSize(430, 200);
        this.setAlwaysOnTop(true);
        this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        this.setLocationRelativeTo(null);
        this.initComponents();
    }

    private void initComponents() {
        this.setLayout(new BorderLayout());
        JPanel pnlCenter = new JPanel(new VerticalFlowLayout(VerticalFlowLayout.TOP));
        this.add(pnlCenter, BorderLayout.CENTER);
        // 第一行配置 两个端口 缓存数量
        JPanel pnlFirst = new JPanel(new FlowLayout(FlowLayout.LEFT));
        pnlFirst.setBorder(BorderFactory.createTitledBorder("基础配置"));
        pnlCenter.add(pnlFirst);

        pnlFirst.add(new JLabel("文本端口:"));
        JTextField tfPortText = new JTextField(6);
        tfPortText.setText(Syncer.PORT_TEXT + "");
        pnlFirst.add(tfPortText);

        pnlFirst.add(new JLabel("文件端口:"));
        JTextField tfPortFile = new JTextField(6);
        tfPortFile.setText(Syncer.PORT_FILE + "");
        pnlFirst.add(tfPortFile);

        JPanel pnlSecond = new JPanel(new FlowLayout(FlowLayout.LEFT));
        pnlSecond.setBorder(BorderFactory.createTitledBorder("程序配置"));
        pnlCenter.add(pnlSecond);

        pnlSecond.add(new JLabel("文本/文件最大数量:"));
        JTextField tfMaxCache = new JTextField(6);
        tfMaxCache.setText(Syncer.MAX_CACHE_ITEM + "");
        pnlSecond.add(tfMaxCache);

        pnlSecond.add(new JLabel("复制间隔(秒:)"));
        JTextField tfMonitorInterval = new JTextField(6);
        tfMonitorInterval.setText(Syncer.MONITOR_INTERVAL + "");
        pnlSecond.add(tfMonitorInterval);

        // 第二行配置 下载地址
        JPanel pnlThird = this.initDownloadBrowser();
        pnlCenter.add(pnlThird);

        JTextField tfDownloadDir = (JTextField) pnlThird.getComponent(0);
        // 保存按钮
        JPanel pnlSouth = new JPanel(new FlowLayout(FlowLayout.RIGHT));
        this.add(pnlSouth, BorderLayout.SOUTH);
        JButton btnSave = new JButton("保存");
        pnlSouth.add(btnSave);
        btnSave.addActionListener(e -> {
            int portText = Integer.parseInt(tfPortText.getText());
            int portFile = Integer.parseInt(tfPortFile.getText());
            int maxCache = Integer.parseInt(tfMaxCache.getText());
            String downloadDir = tfDownloadDir.getText();
            int monitorInterval = Integer.parseInt(tfMonitorInterval.getText());
            UserDefault.getInstance().setValueForKey(KEY_PORT_TEXT, portText);
            UserDefault.getInstance().setValueForKey(KEY_PORT_FILE, portFile);
            UserDefault.getInstance().setValueForKey(KEY_MAX_ITEM, maxCache);
            UserDefault.getInstance().setValueForKey(KEY_DOWNLOAD_DIR, downloadDir);
            UserDefault.getInstance().setValueForKey(KEY_MONITOR_INTERVAL, monitorInterval);
            listener.onConfigChanged(portText, portFile, maxCache, downloadDir, monitorInterval);
            this.dispose();
        });
    }


    private JPanel initDownloadBrowser() {
        JPanel pnlFirst = new JPanel();
        pnlFirst.setBorder(BorderFactory.createTitledBorder("文件同步路径"));
        pnlFirst.setLayout(new BorderLayout());

        JTextField tfBrowser = new JTextField();
        tfBrowser.setName("JTextField");
//        tfBrowser.setEditable(false);
        String path = UserDefault.getInstance().getValueForKey(KEY_DOWNLOAD_DIR, Syncer.DOWNLOAD_PATH);
        File file = new File(path);
        tfBrowser.setText(file.getAbsolutePath());
        pnlFirst.add(tfBrowser, BorderLayout.CENTER);

        JButton btnOpenFolder = new JButton("打开");
        pnlFirst.add(btnOpenFolder, BorderLayout.WEST);
        btnOpenFolder.addActionListener(e -> Utility.openFolder(tfBrowser.getText()));

        JButton btnSelectFolder = new JButton("浏览");
        pnlFirst.add(btnSelectFolder, BorderLayout.EAST);
        btnSelectFolder.addActionListener(e -> {
            JFileChooser fileChooser = new JFileChooser();
            fileChooser.setDialogTitle("文件同步路径");
            fileChooser.setCurrentDirectory(new File(
                    tfBrowser.getText()));
            fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
            int result = fileChooser.showDialog(this, "确认");
            if (JFileChooser.APPROVE_OPTION == result) {
                String path1 = fileChooser.getSelectedFile().getParent();
                tfBrowser.setText(path1);
//                UserDefault.getInstance().setValueForKey(KEY_DOWNLOAD_DIR, path);
            }
        });
        return pnlFirst;
    }

}
